
#include "nvcsocket.h"
unsigned char connected = 0;
int sockfd = 0;
unsigned char is_connected()
{
	return connected;
}

int connect_to(const char * ip, unsigned short port)
{
	struct hostent * host;
	struct sockaddr_in serv_addr;

	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
		return -1;

	if((host = gethostbyname(ip)) == NULL)
		return -1;

	serv_addr.sin_family=AF_INET;
	serv_addr.sin_port=htons(port);
	serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
	bzero(&(serv_addr.sin_zero),8);

	if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) 
		return -1;

	connected = 1;

	return sockfd;
}
void close_socket()
{
	if(sockfd > 0)
		close(sockfd);
}
int send_data(const char * buf, unsigned int size)
{
	int cnt_sent = 0;
	int total_sent = 0;
	while(is_connected() && (total_sent < size))
	{
		cnt_sent = 0;
		if((cnt_sent = send(sockfd, buf + total_sent, size - total_sent, 0) ) == -1)
		{
			connected = 0;
			break;
		}

		total_sent += cnt_sent;
	}
	return total_sent;
}